package com.learn;

import java.util.Arrays;

public class bubbleSort {
    public  static void bubbleSort1(int[] array) {
        int len = array.length;
        if (len <= 1) {
            return;
        }

        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
    }

    public static void bubbleSort2(int[] array) {
        int len = array.length;
        if (len <= 1) {
            return;
        }

        for (int i = 0; i < len; i++) {
            //用来标志是否需要继续排序，true表示已经排好序，false表示还需继续排序
            boolean flag = true;
            for (int j = 0; j < len - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    flag = false;
                }
            }
            if(flag){
                break;
            }
        }
    }

    public static void main(String[] args) {
        int[] array = {3,1,7,4,9,6};
        bubbleSort2(array);
        System.out.println(Arrays.toString(array));
    }
}
